package com.metagurukul.informer.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.metagurukul.informer.service.QuestionsService;
import com.metagurukul.informer.valueObjects.Questions;

/**
 * Servlet implementation class Questions
 */
@WebServlet("/Questions")
public class ShowQuestions extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ShowQuestions() {
        super();
        // TODO Auto-generated constructor stub
    }
    protected void doPerform(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	//response.setContentType("text/html");
		//PrintWriter out=response.getWriter();
		try {
			//System.out.println("asdas");
			// TODO Auto-generated method stub
			
			RequestDispatcher rd=null;
			QuestionsService service=new QuestionsService();
			ArrayList<Questions> listOfQuestions=null;
			if((listOfQuestions=service.getQuestions())!=null){
				HttpSession session=request.getSession();
				session.setAttribute("pageName", "home");
				session.setAttribute("listOfQuestions", listOfQuestions);
				rd=request.getRequestDispatcher("/RetrieveQuestion.jsp");
			} else {
				rd=request.getRequestDispatcher("/Error.jsp");
			}
			rd.forward(request,response);
		} catch (SQLException e) {
			//out.println(e);
			response.sendRedirect("Error.jsp");
		}
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPerform(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPerform(request,response);
	}

}
